#include "comm/znx_defs.h"

void test_bool()
{
    znx_bool_t b = ZNX_TRUE;
    assert(b);

    b = ZNX_FALSE;
    assert(!b);
}

void test_abs_and_swap()
{
    assert(ZNX_ABS(100) == 100 && ZNX_ABS(-100) == 100);

    int a = 10;
    int b = 20;
    int tmp;
    ZNX_SWAP(a, b, tmp);
    assert(a == 20 && b == 10);
}

void test_max_and_min()
{
    assert(ZNX_MAX(1, 10) == 10);

    assert(ZNX_MIN(1, 100) == 1);
}

void test_align()
{
    assert(ZNX_ALIGN(0, ZNX_PAGE_SIZE) == 0);
    assert(ZNX_ALIGN(1, ZNX_PAGE_SIZE) == 4096);
    assert(ZNX_ALIGN(4095, ZNX_PAGE_SIZE) == 4096);
    assert(ZNX_ALIGN(4096, ZNX_PAGE_SIZE) == 4096);
    assert(ZNX_ALIGN(4097, ZNX_PAGE_SIZE) == 8192);
    assert(ZNX_ALIGN(8190, ZNX_PAGE_SIZE) == 8192);
    assert(ZNX_ALIGN(8192, ZNX_PAGE_SIZE) == 8192);
    assert(ZNX_ALIGN(8193, ZNX_PAGE_SIZE) == 12288);

    assert(ZNX_ALIGN_PTR(0, ZNX_PAGE_SIZE) == (u_char *)0);
    assert(ZNX_ALIGN_PTR(1, ZNX_PAGE_SIZE) == (u_char *)4096);
    assert(ZNX_ALIGN_PTR(4095, ZNX_PAGE_SIZE) == (u_char *)4096);
    assert(ZNX_ALIGN_PTR(4097, ZNX_PAGE_SIZE) == (u_char *)8192);
}

typedef struct {
    uint64_t        a:38;
} znx_test_type_t;

void test_castbit()
{
    uint64_t a;

    a = 123456789;  // < 2^38 - 1
    znx_test_type_t t = {
        .a = ZNX_CASTBIT(a, 38)
    };
    assert(t.a == a);

    a = 111111111111; // < 2^38 - 1
    t.a = ZNX_CASTBIT(a, 38);
    assert(t.a == a);

    a = 222222222222; // < 2^38 - 1
    t.a = ZNX_CASTBIT(a, 38);
    assert(t.a == a);

    t.a = 100;
    assert(t.a == 100);
}


void test_prefix()
{
    DD("%s", ZNX_PREFIX);
}


int main()
{
    test_bool();
    test_abs_and_swap();
    test_max_and_min();
    test_align();
    test_castbit();
    test_prefix();

    return 0;
}